package ogs.game.core.codec
{
	import com.adobe.serialization.json.JSON;
	
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import ogs.game.core.protocol.GameMsg;
	import ogs.lib.mina.filter.codec.ProtocolEncoderAdapter;
	import ogs.lib.mina.filter.codec.ProtocolEncoderOutput;
	
	public class MsgEncoder extends ProtocolEncoderAdapter
	{
		public function MsgEncoder()
		{
			super();
		}
		
		override public function encode(message:Object, out:ProtocolEncoderOutput):void
		{
			var msg:GameMsg = GameMsg(message);
			var dataStr:String = JSON.encode(msg.body);
			
			var buffer:ByteArray = new ByteArray();
			buffer.endian = Endian.LITTLE_ENDIAN;
			buffer.writeInt(msg.pcode);
			buffer.writeInt(dataStr.length);
			buffer.writeUTFBytes(dataStr);

			out.write(buffer);
		}

	}
}